#include "stdafx.h"
#include "VImageViewerMainWidow.h"
#include <QObject>
#include <loadFile.h>
#include "elemCont.h"
TElemCont ELEM_CONT;
#include "exprDict.h"
#include "dicomDict.h"
#include "overlayDict.h"
#include <QtPlugin>
//Q_IMPORT_PLUGIN(qjpeg4)

void createDicts()
{
	dicom_dict::TDicomDict::getDict()->loadDict( "expr//dictionary.xml", "expr//local.xml" );
	TExpressionDict::getDict()->loadDict("expr//expressions.xml");
	TOverlayDict::getDict()->loadDict("expr//overlay.xml");
}

//
void saveDicts()
{
	dicom_dict::TDicomDict::getDict()->saveDict("expr//local.xml");
	TExpressionDict::getDict()->saveDict("expr//expressions.xml");
	TOverlayDict::getDict()->saveDict("expr//overlay.xml");
}

void SetCodec(){
	//QTextCodec * russian =QTextCodec::codecForName("CP1251"); 
	QTextCodec * russian = QTextCodec::codecForName("CP1251"); 
	QTextCodec::setCodecForTr(russian);
	QTextCodec::setCodecForCStrings(russian);
}

int main(int argc, char *argv[]){		
	char * s = new char [1];	
	sprintf(s,"", "%d");
	delete s;		
	qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
	SetCodec();
	Q_INIT_RESOURCE(test_imageviewer);		
	QApplication a(argc, argv);		
	if(argc > 1){
		HWND hwnd = FindWindowA((LPCSTR)NULL, "Vidar-XRAY");
		//hwnd = winId();	
		if (hwnd != NULL) {
			COPYDATASTRUCT cd;
			char * buffer = new char [255];				
			unsigned short Cmd = 5004;
			memcpy(buffer, &Cmd, 2);
			unsigned char size = 250;
			memcpy(buffer + 2, &size, 1);			
			strncpy(buffer + 3, argv[1], size);
			cd.lpData = buffer;
			cd.cbData= size ;//sizeof(*buffer);
			::SendMessage(hwnd, WM_COPYDATA, 0, (LPARAM) &cd);			
			return 0;		
		}
	}
	QDir::setCurrent(QCoreApplication::applicationDirPath());
	createDicts();
 	VImageViewerMainWindow w;		
	a.setWindowIcon( QIcon(":/images/main.PNG"));			
	QTextCodec::setCodecForCStrings(QTextCodec::codecForName("Windows-1251")); 
	QStringList list_path;
	QDir dir = QDir(qApp->applicationDirPath() + "/plugins/");
	list_path << dir.absolutePath () << qApp->libraryPaths ();
	qApp->setLibraryPaths( list_path  );	
	GdiplusStartupInput gdiplusStartupInput; 
	ULONG_PTR gdiplusToken; 
	GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);		
	dir.mkdir (QCoreApplication::applicationDirPath() + QString("\\saveImage"));
	char  buff[10];
	QString Path = getWindowAppPath();				
	QString FileName = Path + QString("\\xray.INI");
	BOOLEAN isRomanDownloader = GetPrivateProfileInt("downloadSettings",  "isRomanDownloader", 0, FileName.toAscii().constData());
	_itoa_s(isRomanDownloader, buff, 10);
	WritePrivateProfileStringA("downloadSettings", "isRomanDownloader", buff , FileName.toAscii().constData());
	int numberThreads = GetPrivateProfileInt("downloadSettings",  "numberThreads", 2, FileName.toAscii().constData());
	if(numberThreads <=0)
		numberThreads = 2;
	if(numberThreads > 50)
		numberThreads = 50;	
	_itoa_s(numberThreads, buff, 10);
	WritePrivateProfileStringA("downloadSettings", "numberThreads", buff, FileName.toAscii().constData());
	char * programName = new char [1024];
	GetPrivateProfileString ("downloadSettings",  "programName", "DownloadManagerC.exe", programName, 256 , FileName.toAscii().constData());
	WritePrivateProfileStringA("downloadSettings", "programName", programName, FileName.toAscii().constData());	
	int TEN_BITS_STATE = GetPrivateProfileInt("Setup",  "10Bits", 0, FileName.toAscii().constData());
	if(TEN_BITS_STATE <=0)
		Panel::TEN_BITS_STATE = false;
	else
		Panel::TEN_BITS_STATE = true;
	 _itoa_s(TEN_BITS_STATE, buff, 10);
	WritePrivateProfileStringA("Setup", "10Bits",buff, FileName.toAscii().constData());	
	if(argc > 1){
		if(isRomanDownloader){			
			getFromRomanManager(argv[1], programName, numberThreads);			
		}else{
			w.onTRYDOWNLOADFILESFASTFromFileVREF(argv[1]);				
		}		
		QStringList fileNames;
		fileNames << QCoreApplication::applicationDirPath() + QString("\\saveImage");	
		//fileNames << QString("F:\\Kilin_Oleg_Stanislavovich___2010-10-11");		
		TLoadFilesDialog loadFilesDialog(fileNames);
		loadFilesDialog.move(w.pos().x() + w.width() / 2, w.pos().y() + w.height() / 2);
		loadFilesDialog.exec();			
	}	
	delete programName;
	ContainerPanel::SetAutoFillTopPanels();	
	w.setWindowTitle("Vidar-XRAY");	
	w.setFocus();	
	w.showNormal();
	ContainerPanel::SetAutoFillTopPanels();
	FileName = Path + QString("\\settings.ini");	

	Panel::isGrayMonitor  = GetPrivateProfileInt("style",  "isGrayMonitor", 0, FileName.toAscii().constData());
	_itoa_s(Panel::isGrayMonitor, buff, 10);
	WritePrivateProfileStringA("style", "isGrayMonitor", buff , FileName.toAscii().constData());
	if(Panel::isGrayMonitor){
		char * store = new char[100];
		GetPrivateProfileString ("style",  "menuFontSize", "20", store, 100 , FileName.toAscii().constData());	
		WritePrivateProfileString("style", "menuFontSize", store, FileName.toAscii().constData());
		QString size(store);
		delete store;	 
		QString menuStyle(QString(		
			"QWidget{"
			"font: "  + size + "px;"
			//"background-color: #ABABAB;"
			//"border: 2px solid black;"
			"}"
			"QMenu::item {"
			"font: bold " + size + "px;"
			//"background-color: #ABABAB;"
			//"border: 2px solid black;"
			"}"
			"QMenuBar {"
			"font: bold " + size + "px;"
			//"background-color: #ABABAB;"
			//"border: 2px solid black;"
			"}"
			"QMenuBar::item {"
			"font: bold " + size + "px;"
			//"background-color: #ABABAB;"
			//"border: 2px solid black;"
			"}"
			"QMenu{"
			"font: bold "  + size + "px;"
			//"background-color: #ABABAB;"
			//"border: 2px solid black;"
			"}"
			"QToolTip{"
			"font: "  + size + "px;"
			//"background-color: #ABABAB;"
			//"border: 2px solid black;"
			"}"
			"QLabel{"
			"font: "  + size + "px;"
			//"background-color: #ABABAB;"
			//"border: 2px solid black;"
			"}"
			"QString{"
			"font: "  + size + "px;"
			//"background-color: #ABABAB;"
			//"border: 2px solid black;"
			"}"			
			"QString{"
			"font: "  + size + "px;"
			//"background-color: #ABABAB;"
			//"border: 2px solid black;"
			"}"
			
			



			));
		a.setStyleSheet(menuStyle);
	}
	int value =  a.exec();
	GdiplusShutdown(gdiplusToken);
	saveDicts();	
	return value;
}

